package com.vmladenov.cook.core;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Environment;
import android.os.Handler;
import android.widget.ImageView;

import com.vmladenov.cook.CustomApplication;
import com.vmladenov.cook.core.db.DataHelper;
import com.vmladenov.cook.core.html.SmallPreview;

public final class Helpers
{
	public static DataHelper getDataHelper()
	{
		return CustomApplication.getInstance().getDataHelper();
	}

	public static boolean isOnline(Context context)
	{
		ConnectivityManager cm = (ConnectivityManager) (context
				.getSystemService(Context.CONNECTIVITY_SERVICE));
		NetworkInfo info = cm.getActiveNetworkInfo();
		return info != null && info.isConnected() && !info.isRoaming();
	}

	public static Bitmap getImage(String relativeUrl)
	{
		try
		{
			URL imageUrl = new URL(GlobalStrings.SiteUrl + relativeUrl);
			HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
			conn.setDoInput(true);
			conn.connect();
			InputStream is = conn.getInputStream();

			return BitmapFactory.decodeStream(is);
		}
		catch (IOException e)
		{
		}
		return null;
	}

	public static void setImageFromUrlAsync(ImageView image, final String relativeUrl)
	{
		if (relativeUrl == null || relativeUrl == "") return;
		final Handler handler = new Handler();
		new Thread(new ParameterizedRunnable<ImageView>(image) {

			@Override
			public void ParameterizedRun(ImageView imageParam)
			{
				final Bitmap bitmap = Helpers.getImage(relativeUrl);
				handler.post(new ParameterizedRunnable<ImageView>(imageParam) {
					@Override
					public void ParameterizedRun(ImageView imageParam)
					{
						imageParam.setImageBitmap(bitmap);
					}
				});
			}
		}).start();
	}

	public static void setImageInPreviewAndStoreAsync(ImageView image, final SmallPreview preview)
	{
		if (preview.getImageUrl() == null || preview.getImageUrl() == "") return;
		final Handler handler = new Handler();
		new Thread(new ParameterizedRunnable<ImageView>(image) {

			@Override
			public void ParameterizedRun(ImageView imageParam)
			{
				final Bitmap bitmap = Helpers.getSmallImage(imageParam.getContext(),
						preview.getImageUrl());
				// Helpers.getImage(preview.getImageUrl());
				handler.post(new ParameterizedRunnable<ImageView>(imageParam) {
					@Override
					public void ParameterizedRun(ImageView imageParam)
					{
						imageParam.setImageBitmap(bitmap);
						preview.setCachedImage(bitmap);
					}
				});
			}
		}).start();
	}

	public static void copyFile(InputStream in, OutputStream out) throws IOException
	{
		byte[] buffer = new byte[1024];
		int read;
		while ((read = in.read(buffer)) != -1)
		{
			out.write(buffer, 0, read);
		}
	}

	public synchronized static Bitmap getSmallImage(Context context, String imageUrl)
	{
		String state = Environment.getExternalStorageState();
		if (Environment.MEDIA_MOUNTED.equals(state)
				|| Environment.MEDIA_MOUNTED_READ_ONLY.equals(state))
		{
			String directory = context.getExternalFilesDir(null) + "/images/";
			File directoryFile = new File(directory);
			if (!directoryFile.exists()) directoryFile.mkdir();
			String imageName = imageUrl.substring(imageUrl.lastIndexOf('/') + 1);
			File file = new File(directory, imageName);
			if (file.exists())
			{
				return readFile(directory + imageName);
			}
			else
			{
				if (isOnline(context))
				{
					Bitmap bitmap = Helpers.getImage(imageUrl);
					saveFile(bitmap, directory, imageName);
					return bitmap;
				}
				else
					return null;
			}
		}
		else
		{
			return Helpers.getImage(imageUrl);
		}
		// return null;
	}

	private static Bitmap readFile(String path)
	{
		return BitmapFactory.decodeFile(path);
	}

	private static void saveFile(Bitmap bitmap, String directory, String imageName)
	{
		if (bitmap == null || bitmap.getHeight() < 1) return;
		try
		{
			OutputStream outputStream = new FileOutputStream(directory + imageName);
			BufferedOutputStream bos = new BufferedOutputStream(outputStream);
			bitmap.compress(CompressFormat.JPEG, 90, bos);
			bos.flush();
			bos.close();
			outputStream.close();
		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

}
